﻿using System;
using System.Collections.Generic;
using System.Xml;
using SdmxMl.Common;
using SdmxMl.Helper;

namespace SdmxMl.Structure
{
    /// <summary>
    /// The full target identifier provides details on all of the objects against
    /// which metadata can be reported. The full target identifier is made up of a set
    /// of identifier components - each getting its own child element - which are similar
    /// to the dimensions of a key family: each one indicates that a value will be provided
    /// by the metadata reporter to identify and describe the metadata being reported.
    /// A human-readable name must be provided, which may be provided in multiple,
    /// parallel-language versions. A longer, human-readable name may also be provided
    /// in multiple, language-parallel versions. Annotations may be provided.
    /// </summary>
    public class FullTargetIdentifierType: BaseArtefact
    {
        #region Properties

        private string _urn;
        public virtual string Urn
        {
            get { return _urn; }
            set { _urn = value; }
        }

        private string _uri;
        public virtual string Uri
        {
            get { return _uri; }
            set { _uri = value; }
        }

        private IdentifierComponentTypeList _identifierComponentList;
        public IdentifierComponentTypeList IdentifierComponentList
        {
            get { return _identifierComponentList; }
        }

        
        #endregion // Properties

        #region Construction

        private void Init()
        {
            _identifierComponentList = new IdentifierComponentTypeList();
        }

        public FullTargetIdentifierType()
        {
            Init();

        }
        public FullTargetIdentifierType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr)
            : base(nodeArtefact, nsmgr)
        {
            Init();
            _urn = Xmlhelper.GetAtt(nodeArtefact, "urn");
            _uri = Xmlhelper.GetAtt(nodeArtefact, "uri");

            string query = nsmgr.VersionSdmx == SdmxVersion.SDMX_20 ?
                "./structure:IdentifierComponent" : "./structure:IdentifiableObjectTarget";
            XmlNodeList nodeList = nodeArtefact.SelectNodes(query, nsmgr);
            if (nodeList.Count > 0)
            {
                foreach (XmlNode nd in nodeList)
                {
                    IdentifierComponentType it = new IdentifierComponentType(nd, nsmgr);
                    _identifierComponentList.Add(it);
                }
            }
            if (nsmgr.VersionSdmx == SdmxVersion.SDMX_21)
                Name.UpdateDefaultText("SDMX2.1 undefined");
        }
        

        #endregion //Construction

        #region XML serialize

        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            xw.WriteStartElement(prefix, tag, null);
            // id required
            if (string.IsNullOrEmpty(Id))
                Id = "undefined";
            xw.WriteAttributeString("id", Id);
            Xmlhelper.WriteAttributeIfExist(xw, "urn", Urn);
            Xmlhelper.WriteAttributeIfExist(xw, "uri", Uri);

            Name.WriteXml(xw, prefix, "Name");
            Description.WriteXml(xw, prefix, "Description");

            foreach (IdentifierComponentType it in _identifierComponentList)
                it.WriteXml(xw, prefix, "IdentifierComponent");

            // Annotations
            AnnotationList.WriteXml(xw, prefix, null);
            xw.WriteEndElement();
        }
        
        #endregion //XML serialize

    }
}
